import {Component, OnInit} from '@angular/core';
import {DeviceService} from "../device-component/device-component.service";
import {FormBuilder, FormGroup, Validators} from "@angular/forms";
import {Router} from "@angular/router";

@Component({
  selector: 'app-device-add-component',
  templateUrl: './device-add-component.component.html',
  styleUrls: ['./device-add-component.component.scss'],
  providers: [DeviceService]
})
export class DeviceAddComponentComponent implements OnInit {

  private validateForm: FormGroup;

  constructor(private deviceService: DeviceService,
              private formBuilder: FormBuilder,
              private _router: Router) {
  }

  ngOnInit() {
    this.validateForm = this.formBuilder.group({
      id: ['', Validators.required],
      name: ['', Validators.required],
      appId: ['', Validators.required]
    })
  }

  save() {
    if (this.validateForm.valid) {
      this.deviceService.add(this.validateForm.value).subscribe(n => {
        this._router.navigateByUrl('/device');
      }, e => {
        console.log(e)
      });
    }
  }

  reset() {
    this.validateForm.reset();
  }

}
